#!/bin/bash
# chkconfig: 2345 64 36
SERVER_FILE=/a8root/bin/inke_replace_ads/inke_replace_ads_server.py
LOG_DIR=/a8root/var/inke_replace_ads/pid
function start()
{
    python $SERVER_FILE
}

function stop()
{
    num=$(ps -ef | grep 'inke_replace_ads_server.py' | grep -v grep |wc -l)
    if [ $num -eq 0 ]; then
        echo 'inke_replace_ads is already stopped!!!!'
        echo -e '\033[31mStop inke_replace_ads server FAILED\033[0m'
        exit 1
    fi
    pid=$(ps -ef | grep 'inke_replace_ads_server.py' | grep -v grep | awk '{print $2}')
    for p in ${pid}
    do
        #kill -QUIT ${p}
        kill -9 ${p}
    done
    rm -rf $LOG_DIR
}

case "$1" in
    start)
    start
    status=`echo $?`
    if [ $status -eq 0 ]; then
        echo -e '\033[32mStart inke_replace_ads http server SUCCESS\033[0m'
    else
        echo -e '\033[31mStart inke_replace_ads http server FAILED\033[0m'
    fi
        ;;
    stop)
    stop
    status=`echo $?`
    if [ $status -eq 0 ]; then
        echo -e '\033[32mStop inke_replace_ads http server SUCCESS\033[0m'
    else
        echo -e '\033[31mStop inke_replace_ads http server FAILED\033[0m'
    fi
        ;;
    restart)
    stop
    sleep 2
    start
        ;;
esac
